//
//  HMEmoticonToolBar.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/30.
//  Copyright © 2016年 itcast. All rights reserved.
//  表情页面 不同类型表情分页按钮

import UIKit
//用枚举项保存四个按钮
enum HMEmotionType: Int {
    case Recent = 0, Default, Emoji, Lxh
}

class HMEmoticonToolBar: UIStackView {
    
//下一个点击的按钮
    var lastSelectButton: UIButton?

    // 提供给外界设置当前选中按钮index的方法
    var selectSection: Int = 0 {
        
        didSet{
            //记录当前按钮
            let button = viewWithTag(selectSection)as! UIButton
            //如果当前按钮不等于下一次点击的按钮
            if button != lastSelectButton{
                //隐藏当前按钮的selected
                button.selected = true
                //下一次点击的按钮常亮状态开启
                lastSelectButton?.selected = false
                //设置下一次点击的按钮等于当前按钮
                lastSelectButton = button
            }
        }
    }
    
    // 点击表情类型按钮要执行的闭包
    var emoticonButtonClickClosure: ((type: HMEmotionType)->())?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置ui
    
    private func setupUI(){
        tag = 20000
        distribution = .FillEqually
        // 添加4个按钮
        addChildButton("最近", bgImgNamed: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", bgImgNamed: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgImgNamed: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImgNamed: "compose_emotion_table_right", type: .Lxh)
    }
    
    /// 添加按钮的公共方法
    ///
    /// - parameter title:      <#title description#>
    /// - parameter bgImgNamed: <#bgImgNamed description#>
    private func addChildButton(title: String, bgImgNamed: String, type: HMEmotionType){
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置tag
        button.tag = type.rawValue
        // 设置文字以及字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle(title, forState: UIControlState.Normal)
        
        // 设置不同状态下的文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_selected"), forState: UIControlState.Selected)
        
        // 添加到stackView里面
        addArrangedSubview(button)
    }
    
    /**
     点击事件
     
     - parameter button: <#button description#>
     */
    @objc private func childButtonClick(button: UIButton) {
        
        // 如果用户点击的按钮就是当前选中的按钮的话就不执行逻辑
        if lastSelectButton == button {
            return
        }
        // 执行闭包
        emoticonButtonClickClosure?(type: HMEmotionType(rawValue: button.tag)!)
        
        
    }

}
